package com.linxcool.action;

import java.util.Observable;
import java.util.Observer;

/**
 * 动作观察者
 * <p>默认完成一次观察后删除该观察者
 * @author: linxcool.hu
 */
public abstract class ActionObserver implements Observer{
	protected ActionSupport action;
	protected boolean flag;

	/**
	 * 构造器<br>
	 * 注意：默认完成一次观察后删除该观察者
	 * 
	 * @param action
	 */
	public ActionObserver(ActionSupport action) {
		this.action = action;
		this.action.addObserver(this);
		this.flag = false;
	}

	public void setCycleUtilization() {
		flag = true;
	}

	@Override
	public void update(Observable observable, Object data) {
		int result=(Integer) data;
		switch (result) {
		case ActionSupport.SUCCESS:onSuccess(); break;
		case ActionSupport.ERROR:onError(); break;
		case ActionSupport.FAILURE:onFailure();break;
		}
		if(!flag)action.deleteObserver(this);
	}
	
	/** 数据加载成功 */
	protected abstract void onSuccess();
	
	/** 数据加载失败 */
	protected abstract void onError();
	
	/** 数据加载异常 */
	protected abstract void onFailure();
	
}
